﻿//---------------------------------------------------------------------------

#ifndef h_globalH
#define h_globalH
//---------------------------------------------------------------------------

class HThread : public TThread
{

public :

   __fastcall HThread(bool CreateSuspended)
      : TThread(CreateSuspended)
   {
      FExitEvent = new TSimpleEvent(false);
   }
   virtual __fastcall ~HThread(void)
   {
      Exit();
      WaitFor();
      delete FExitEvent;
   }
   void __fastcall Exit(void)
   {
      Terminate();
      FExitEvent->SetEvent();
   }

protected :

   TSimpleEvent *FExitEvent;
};

class HComm
{
public :

// 返回 -1 ：其他错误，需要检查 ErrCode
// 返回 -2 ：超时
// 返回 -3 ：NumOfEvent 超过 8
// 返回 -4 ：对方关闭
// 返回 -5 ：内部故障

   static void __fastcall Init(void);
   static void __fastcall Close(void);
   
   static int __fastcall Accept(SOCKET Sock, SOCKET &MsgSock,
      string &IpAddr, int TimeOut, DWORD *ErrCode = NULL,
      const HANDLE *EventHandles = NULL, int NumOfEvent = 0);

   static int __fastcall Connect(SOCKET Sock, const char *IpAddr,
      int Port, int TimeOut, DWORD *ErrCode = NULL,
      const HANDLE *EventHandles = NULL, int NumOfEvent = 0);

   static int __fastcall Read(SOCKET Sock, void *Buf, int Len,
      int TimeOut, DWORD *ErrCode = NULL,
      const HANDLE *EventHandles = NULL, int NumOfEvent = 0);

   static int __fastcall Write(SOCKET Sock, const void *Buf, int Len,
      int TimeOut, DWORD *ErrCode = NULL,
      const HANDLE *EventHandles = NULL, int NumOfEvent = 0);

   static int __fastcall ReadByLength(SOCKET Sock, void *Buf, int &Len,
      int TimeOut, DWORD *ErrCode = NULL,
      const HANDLE *EventHandles = NULL, int NumOfEvent = 0);

   static int __fastcall WriteByLength(SOCKET Sock, const void *Buf, int Len,
      int TimeOut, DWORD *ErrCode = NULL,
      const HANDLE *EventHandles = NULL, int NumOfEvent = 0);
};

struct HColumnInfo
{
   UTF8String Name;
   UTF8String Type;
   int Length;
   int Scale;
   bool IsKey;
} ;

struct HServerInfo
{
   UTF8String Name;
   UTF8String IpAddr;
   UTF8String Port;
};

struct HJobListInfo
{
   UTF8String Name;
   UTF8String GroupId;
   UTF8String Type;
   UTF8String Command;
   UTF8String CommandUpper;
   UTF8String ExternJob;
   vector<UTF8String> ExternJobList;
   UTF8String Group;
   UTF8String Tag;
   UTF8String Description;
   int InDegreeWithinGroup;

   set<UTF8String> SuccJobSet;

   UTF8String Status;
   UTF8String StartTime;
   UTF8String EndTime;
   UTF8String ElapsedTime;
   UTF8String Partition;
   UTF8String TotalElapsed;
   UTF8String Imbalanced;
   int IdleTimeSum;
   bool PartWaiting;
   bool PartRunning;
   bool PartSkipped;
   bool PartFail;
   bool PartSuccess;
   bool PartUnknown;

   __fastcall HJobListInfo()
      : InDegreeWithinGroup(0), IdleTimeSum(0),
        PartWaiting(false), PartRunning(false), PartSkipped(false),
        PartFail(false), PartSuccess(false), PartUnknown(false) 
   {}
};

struct HBatListInfo
{
   int Idx;
   UTF8String Name;
   UTF8String Description;
   UTF8String Status;
   UTF8String StartTime;
   UTF8String EndTime;
   UTF8String ElapsedTime;
   UTF8String BatchDate;
   UTF8String NextScheduleDate;
};

struct HPartTimeInfo
{
   UTF8String PartName;
   UTF8String StartTime;
   UTF8String EndTime;
};

struct HGanttInfo
{
   UTF8String Name;
   int Qty;
   TDateTime StartTime;
   TDateTime EndTime;
   bool StartFlag;
   bool EndFlag;
   UTF8String Hint;
   int Left;
   int Right;
   int ErrorRate;
   HGanttInfo()
      : Qty(1), StartFlag(false), EndFlag(false), Left(0), Right(0), ErrorRate(0)
   {}
};

struct HNavInfo
{
   int ServerIdx;
   UTF8String Caption;
   UTF8String GroupName;
   int HScrollPosition;
   int VScrollPosition;
   char JobNameDelimiter[2];
   HNavInfo()
      : ServerIdx(-1), HScrollPosition(0), VScrollPosition(0)
   {
      strcpy(JobNameDelimiter, ":");
   }
};

bool __fastcall CheckExpr(const UTF8String &Expr, UTF8String &ErrorString);
void __fastcall SetStringToMemo(TMemo *Memo, const UTF8String &String);
UTF8String __fastcall GetStringFromMemo(TMemo *Memo);
bool __fastcall FileExist(const UTF8String &FileName);
void __fastcall IntToFontStyle(int Int,
   Set<TFontStyle, fsBold, fsStrikeOut> &Style);
void __fastcall FontStyleToInt(int &Int,
   const Set<TFontStyle, fsBold, fsStrikeOut> &Style);
UTF8String __fastcall GetDateTime(const char *s);
UTF8String __fastcall DiffTime(const UTF8String &StartTime,
   const UTF8String &EndTime, int SubDelta = 0);
void __fastcall DiffTime(const UTF8String &StartTime,
   const UTF8String &EndTime, unsigned short &Hour, unsigned short &Min,
   unsigned short &Sec);
void __fastcall DiffTime(const TDateTime &StartTime,
   const TDateTime &EndTime, unsigned short &Hour, unsigned short &Min,
   unsigned short &Sec);
int __fastcall DiffTimeSec(const UTF8String &StartTime,
   const UTF8String &EndTime);
int __fastcall DateTimeToSec(const TDateTime &DateTime);
TDateTime __fastcall SecToDateTime(int Sec);
UTF8String __fastcall FormatDateTime(const UTF8String &DateTime);
bool __fastcall ToDateTime(const UTF8String &Str, TDateTime &DateTime);
bool __fastcall PosInRect(int XPos, int YPos, const TRect &Rect);
int __fastcall StatusToInt(const UTF8String &Status);
void __fastcall CreateSpecialDateList(TComboBox *ComboBox,
   const vector<string> &Dates);
bool __fastcall StringToDateTime(const UTF8String &Str, TDateTime &DateTime);
int __fastcall SubDateTime(const TDateTime &DateTime1, const TDateTime &DateTime2);
UTF8String __fastcall DateTimeToString1(const TDateTime &DateTime);
UTF8String __fastcall DateTimeToString2(const TDateTime &DateTime);
UTF8String __fastcall DateTimeToString3(const TDateTime &DateTime);

template<typename V, typename T>
   bool find_name(const V &Vector, const T &Name, int &i)
{
   for( i = 0; i < Vector.size(); i++ )
   {
      if( Vector[i].Name == Name )
         break;
   }
   if( i == Vector.size() )
      return false;
   else
      return true;
}

#endif
